﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Transactions;

namespace BusinessLogic
{
    public static class LoaiTheVip
    {

        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;

        #region select
        public static Entities.LoaiTheVip[] Select(string hanhdong)
        {
            Entities.LoaiTheVip[] list = null;
            try
            {
                string sql = Common.Constants.LoaiTheVip.Select;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("HanhDong", SqlDbType.VarChar, 50).Value = hanhdong;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.LoaiTheVip loaithevip = new Entities.LoaiTheVip();
                    loaithevip.LoaiTheVipID = int.Parse(dr[Common.Constants.LoaiTheVip.LoaiTheVipID].ToString());
                    loaithevip.MaLoaiTheVip = dr[Common.Constants.LoaiTheVip.MaLoaiTheVip].ToString();
                    loaithevip.TenLoaiThe = dr[Common.Constants.LoaiTheVip.TenLoaiThe].ToString();
                    loaithevip.GiaTriThe = Common.Utilities.FormatMoney(dr[Common.Constants.LoaiTheVip.GiaTriThe].ToString());
                    loaithevip.SoDiemThuong = dr[Common.Constants.LoaiTheVip.SoDiemThuong].ToString();
                    loaithevip.GhiChu = dr[Common.Constants.LoaiTheVip.GhiChu].ToString();
                    loaithevip.Deleted = bool.Parse(dr[Common.Constants.LoaiTheVip.Deleted].ToString());
                    arr.Add(loaithevip);
                }
                return (Entities.LoaiTheVip[])arr.ToArray(typeof(Entities.LoaiTheVip));
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.LoaiTheVip[0]; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }

        }
        #endregion

        #region insert update
        public static int InsertUpdate(Entities.LoaiTheVip loaithevip)
        {
            TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.LoaiTheVip.InsertUpdate;
                    cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(Common.Constants.LoaiTheVip.HanhDong, SqlDbType.NVarChar, 50).Value = loaithevip.HanhDong;
                    cmd.Parameters.Add(Common.Constants.LoaiTheVip.LoaiTheVipID, SqlDbType.Int).Value = loaithevip.LoaiTheVipID;
                    cmd.Parameters.Add(Common.Constants.LoaiTheVip.MaLoaiTheVip, SqlDbType.VarChar, 50).Value = loaithevip.MaLoaiTheVip;
                    cmd.Parameters.Add(Common.Constants.LoaiTheVip.TenLoaiThe, SqlDbType.NVarChar, 50).Value = loaithevip.TenLoaiThe;
                    cmd.Parameters.Add(Common.Constants.LoaiTheVip.GiaTriThe, SqlDbType.Float).Value = loaithevip.GiaTriThe;
                    cmd.Parameters.Add(Common.Constants.LoaiTheVip.SoDiemThuong, SqlDbType.Float).Value = loaithevip.SoDiemThuong;
                    cmd.Parameters.Add(Common.Constants.LoaiTheVip.GhiChu, SqlDbType.NVarChar, 200).Value = loaithevip.GhiChu;
                    cmd.Parameters.Add(Common.Constants.LoaiTheVip.Deleted, SqlDbType.Bit).Value = loaithevip.Deleted;
                    rt = cmd.ExecuteNonQuery();
                    cn.Close();
                    if (loaithevip.HanhDong == "Insert")
                    {
                        LuuVietHeThong.LuuViet(
                            "Thêm loại thẻ vip",
                            loaithevip.TaiKhoan.MaNhanVien,
                            "Thêm loại thẻ vip mới " + loaithevip.MaLoaiTheVip,
                            loaithevip.TaiKhoan.TenDangNhap,
                            DateTime.Now.ToString("dd/MM/YYYY"));
                    }
                    else
                    {
                        LuuVietHeThong.LuuViet(
                            "Sửa loại thẻ vip",
                            loaithevip.TaiKhoan.MaNhanVien,
                            "Sửa loại thẻ vip " + loaithevip.MaLoaiTheVip,
                            loaithevip.TaiKhoan.TenDangNhap,
                            DateTime.Now.ToString("dd/MM/YYYY"));
                    }
                    ts.Complete();
                    return rt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                return 0;
            }

        }
        #endregion

        #region delete
        public static int Delete(Entities.LoaiTheVip loaithevip)
        {
            TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.LoaiTheVip.Delete;
                    SqlConnection cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(Common.Constants.LoaiTheVip.LoaiTheVipID, SqlDbType.Int).Value = loaithevip.LoaiTheVipID;
                    rt = cmd.ExecuteNonQuery();
                    LuuVietHeThong.LuuViet(
                        "Xóa loại thẻ vip",
                        loaithevip.TaiKhoan.MaNhanVien,
                        "Xóa loại thẻ vip " + loaithevip.MaLoaiTheVip,
                        loaithevip.TaiKhoan.TenDangNhap,
                        DateTime.Now.ToString("dd/MM/YYYY"));
                    ts.Complete();
                    cn.Close();
                    return rt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                return 0;
            }

        }
        #endregion
    }
}
#endregion